//
//  Selection_CollectionCell.m
//  LoveTravel
//
//  Created by MS on 16-1-21.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "Selection_CollectionCell.h"
#import "LGCollectionViewCell.h"
@interface Selection_CollectionCell ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (nonatomic,strong)  NSTimer * timer;
@property (nonatomic,assign) NSInteger pages;
@end

@implementation Selection_CollectionCell

-(void)setItemsArray:(NSArray *)itemsArray
{
    _itemsArray = itemsArray;
    
    
    [self.timer invalidate];
    self.timer = nil;
    _pages = 0;
    [_collectionView reloadData];
    
    if (itemsArray.count>1) {
        [self startTime];
    }
}





-(void)creatCollectionView
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.autoresizesSubviews = YES;
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:collectionView];
    collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView = collectionView;
    collectionView.delegate = self;
    collectionView.dataSource  = self;
    collectionView.bounces = NO;
    collectionView.pagingEnabled = YES;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemsArray.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LGCollectionViewCell * item = [LGCollectionViewCell cellWithcollectionView:collectionView cellForItemAtIndexPath:indexPath];
    item.backgroundColor = [UIColor redColor];
    item.collectionItemData = self.itemsArray[indexPath.item];
    
    return item;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(collectionView.width,collectionView.height);
}


-(void) startTime
{
    
    NSTimer * timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    self.timer = timer ;
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}


-(void) autoScroll
{
    _pages++;
    
    if (_pages==self.itemsArray.count) {
        _pages=0;
    }
    
    [self.collectionView setContentOffset:CGPointMake(_pages*self.frame.size.width, 0) animated:YES];
    
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer invalidate];
    self.timer = nil;
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
   
    [self startTime];
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(selection_CollectionCellDidClick:recomm_collection:index:)]) {
        [self.delegate selection_CollectionCellDidClick:self recomm_collection:self.itemsArray[indexPath.item] index:indexPath.item];
    }    
}

-(void)awakeFromNib
{
    [self creatCollectionView];
     self.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
   _lineView.backgroundColor = [UIColor colorWithRed:0.91f green:0.91f blue:0.91f alpha:1.00f];

}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.contentView setNeedsLayout];
    [self.contentView layoutIfNeeded];
    
    _collectionView.frame =self.bounds;//CGRectMake(0, 0, self.contentView.width, 200);
    _collectionView.height = self.height-10;
    
}

@end
